
package monitortest;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.Scanner;

/**
 *
 * @author the Dope Factory
 */
public class ProxyRead implements Runnable{
    Socket sck;                
    InetAddress hostAddr;      
    InetSocketAddress sckAddr; 
    Scanner inSck;              
//    PrintWriter outSck;  
    
    Thread process;
    
    public void connectProxy(String hostName, int port){
        try{
            //get a socket and try connection to server
            sck=new Socket();
            //get host by name
            hostAddr=InetAddress.getByName(hostName);
            sckAddr=new InetSocketAddress(hostAddr,port);
            //connection: may suspend
            sck.connect(sckAddr);
            //build in and out channells
            inSck=new Scanner(sck.getInputStream());
//            outSck = new PrintWriter(sck.getOutputStream(),true);
        }
        catch(IOException e){
            System.out.println(e); // print exception on console
        }
    }
    
    /**
     * wait for data from channell
     * @return a string up to new line
     */
    public String read(){
        String message;
        message=inSck.nextLine();
        return message;
    }
    
    /**
     * Send data to channel
     * @param String message to be output
     */
//    public void channelOut(String messaggio){
//        outSck.println(messaggio); 
//    }
//    /** close connection */
//    public void closeChannel(){
//        try{
//            sck.close(); // chiude la connessione
//        }
//        catch(IOException e){
//            System.out.println(e);
//        }
//    }
//    
    
    /** 
     * chiude la connessione 
     */
    public void closeChannel(){
        try{
        sck.close(); // chiude la connessione
        }catch(IOException e){
            System.out.println(e);
        }
    }

    @Override
    public void run() {
        process=new Thread(this,"Proxy reading");
        process.setPriority(Thread.MAX_PRIORITY);
        ManagementSerial man=new ManagementSerial();
        String temp=read();
        if(temp!=null){
            man.analyze(temp);
        }
    }
}
